package chapter4;

import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

public class Chapter4Test {
    public static void main(String[] args) {
        Trader raoul = new Trader("Raoul", "Cambridge");
        Trader mario = new Trader("Mario", "Milan");
        Trader alan = new Trader("Alan", "Cambridge");
        Trader brian = new Trader("Brian", "Cambridge");

        List<Transaction> transactions = Arrays.asList(
                new Transaction(brian, 2011, 300),
                new Transaction(raoul, 2012, 1000),
                new Transaction(raoul, 2011, 400),
                new Transaction(mario, 2012, 710),
                new Transaction(mario, 2012, 700),
                new Transaction(alan, 2012, 950)
        );

        /**
         * 1.找出2011年发生的所有交易  并按交易额排序
         */
        List<Transaction> traders_2011 = transactions.stream()
                .filter(t -> t.getYear() == 2011)
                .sorted(Comparator.comparingInt(Transaction::getValue))
                .collect(toList());
        for(Transaction transaction: traders_2011){
            System.out.println(transaction);
        }

        seperatorLine();

        /**
         * 2.交易员都在哪些不同的城市工作过
         */
        List<String> traders_city = transactions.stream()
                .map( t->t.getTrader().getCity())
                .distinct()
                .collect(toList());
        for(String str: traders_city){
            System.out.println(str);
        }

        seperatorLine();

        /**
         * 3.查找所有来自于剑桥的交易员 并按姓名排序
         */
        List<Trader> trader_name = transactions.stream()
                .map(Transaction::getTrader)
                .filter(trader -> trader.getCity().equals("Cambridge"))
                .distinct()
                .sorted(Comparator.comparing(Trader::getName))
                .collect(Collectors.toList());


        for(Trader trader: trader_name){
            System.out.println(trader.getName());
        }
        seperatorLine();
        /**
         * 4.返回所有交易员的姓名字符串 按字母顺序排序
         */

        String all_trader_name = transactions.stream()
                .map(t -> t.getTrader().getName())
                .distinct()
                .sorted()
                .reduce("",(n1, n2) -> n1 + n2);
        System.out.println(all_trader_name);
        seperatorLine();

        /**
         * 5.有没有交易员是在米兰工作的？
         */
         boolean is_milan_trader = transactions.stream()
                .anyMatch(t -> t.getTrader().getCity().equals("Milan"));
        System.out.println("Is any trader in Milan: " + is_milan_trader);
        seperatorLine();

        /**
         * 6.打印生活在剑桥的交易员的所有交易额
         */
        int sum_of_cambridge = transactions.stream()
                .filter(t -> t.getTrader().getCity().equals("Cambridge"))
                .map( t -> t.getValue())
                .reduce(0,(n1, n2) -> n1 + n2);
        System.out.println(" sum of combridge: " + sum_of_cambridge);
        seperatorLine();

        /**
         * 7.所有交易中 最高的交易额是多少
         */
        Optional<Integer> highestValue = transactions.stream()
                .map(Transaction::getValue)
                .reduce(Integer::max);
        System.out.println("hightest value: "+highestValue.get());
        seperatorLine();

        /**
         * 8.找到交易额最小的交易
         */
        Optional<Transaction> smallestTransaction = transactions.stream()
                .reduce((t1, t2) -> t1.getValue() > t2.getValue() ? t2 : t1);
        System.out.println("smallest trade: " + smallestTransaction.get().getTrader());
    }

    public static void seperatorLine(){
        System.out.println("");
        System.out.println("--------------------------------------------------------------");
        System.out.println("");
    }
}
